<template>
    <div class="my-apps">
        <CardList
            ref="appList"
            :listType="0"
            :menuList="menuList"
            :pageSize="19"
            :menuNameFilter="menuNameFilter"
            @itemClick="itemClick"
            @addAppByBlank="addAppByBlank"
            @addAppByLibrary="addAppByLibrary"
            @addAppByImport="addAppByImport"/>
        <addOrEditApp ref="addOrEditApp" @change="$refs.appList.getData()"/>
        <changeOwnerApp ref="changeOwnerApp" @change="$refs.appList.getData(1)"/>
        <appInfo ref="appInfo" />
        <sale-app-dialog ref="saleAppDlg"/>
    </div>
</template>

<script>
import CardList from "@/components/CardList.vue"
import addOrEditApp from "../components/addOrEditApp.vue"
import appInfo from "../components/appInfo.vue"
import changeOwnerApp from "../components/changeOwnerApp.vue"
import saleAppDialog from './saleAppDialog.vue'
import { openWin } from "@/utils/tool";

export default {
    name: "myApps",
    components: {
      CardList,
      addOrEditApp,
      appInfo,
      changeOwnerApp,
      saleAppDialog
    },
    computed: {},
    data(){
        return {
            queryName:'',
            menuList:[
                {
                    icon:"vcloud-run",
                    name:"运行",
                    callback:(data)=>{
                        const url = `${_ctxPath}/cap4/businessTemplateController.do?method=businessIndex&bussId=${data.id}`
                        openWin(url,data.name,40,30);
                    }
                },
                {
                    icon:"vcloud-xiugai",
                    name:"修改名称",
                    callback:(data)=>{
                        this.$refs.addOrEditApp.open(data)
                    }
                },
                {
                    icon:"vcloud-fasong",
                    name:"上架",
                    callback(data) {
                        this.api.checkPutOnApp(
                            { params: { appId: data.id } },
                            { method: "get", customRequest: true }
                        ).then(({ code, message }) => {
                            if (code !== '200') return this.$alert(message);
                            Promise.all([
                                this.api.getPutOnParams(
                                    { params: { appId: data.id } },
                                    { method: "get", customRequest: true }
                                ),
                                this.api.getGoodsOptions(
                                    { params: { appId: data.id } },
                                    { method: "get", customRequest: true }
                                )
                            ]).then(([{ data: putOnParams }, { data: goodsOptions }]) => {
                                this.$refs.saleAppDlg.pop(putOnParams, goodsOptions);
                            });
                        });
                    }
                },
                {
                    name:"应用导出",
                    group:'1',
                    callback:data=>{
                        this.$parent.openDownloadDialog(data.id,data.name)
                    }
                },
                {
                    name:"从本地更新",
                    group:'1',
                    callback:()=>{
                        this.$parent.openLocalRefreshDialog()
                    }
                },
                {
                    name:"复制应用",
                    group:'2',
                    callback:()=>{
                        this.copyApp()
                    }
                },
                {
                    icon:"vcloud-Modifyowner",
                    name:"修改所属人",
                    group:'2',
                    callback:data=>{
                        this.$refs.changeOwnerApp.open(data)
                    }
                },
                {
                    name:"删除数据",
                    group:'4',
                    callback:(data)=>{
                        this.$confirm('删除后，表单全部数据被删除且无法恢复！如误操作导致数据被误删，请自行负责！', '删除数据', {
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                                closeOnClickModal:false,
                                type: 'warning',
                                beforeClose: (action, instance, done) => {
                                    if (action === 'confirm') {
                                    instance.confirmButtonLoading = true;
                                    instance.confirmButtonText = '执行中...';
                                    this.api.deleteAppData({appId:data.id})
                                        .then(()=>{
                                            this.$message({
                                                type: 'success',
                                                message: '删除成功!'
                                            });
                                        })
                                        .catch(()=>{
                                            this.$message({
                                                message: '删除失败',
                                                type: 'warning'
                                            });
                                        })
                                        .finally(()=>{
                                            done();
                                            this.$nextTick(()=>{
                                                instance.confirmButtonLoading = false;
                                            })
                                        })
                                    } else {
                                        if(instance.confirmButtonLoading === false) done();
                                    }
                                }
                            }).catch(()=>{

                            })
                    }
                },
                {
                    name:"删除应用",
                    group:'4',
                    callback:(data)=>{
                        this.$confirm('删除后，所有表单,报表,业务空间和业务导图将无法使用且无法恢复，您确认要继续删除？', '删除应用', {
                                confirmButtonText: '确定',
                                cancelButtonText: '取消',
                                closeOnClickModal:false,
                                type: 'warning',
                                beforeClose: (action, instance, done) => {
                                    if (action === 'confirm') {
                                    instance.confirmButtonLoading = true;
                                    instance.confirmButtonText = '执行中...';
                                    this.api.deleteApp({appId:data.id})
                                        .then(()=>{
                                            this.$message({
                                                type: 'success',
                                                message: '删除成功!'
                                            });
                                            this.$refs.appList.getData(1)
                                        })
                                        .catch(()=>{
                                            this.$message({
                                                message: '删除失败',
                                                type: 'warning'
                                            });
                                        })
                                        .finally(()=>{
                                            done();
                                            this.$nextTick(()=>{
                                                instance.confirmButtonLoading = false;
                                            })
                                        })
                                    } else {
                                        if(instance.confirmButtonLoading === false) done();
                                    }
                                }
                            }).catch(()=>{

                            })
                    }
                },
                {
                    icon:"vcloud-yingyongshuxing",
                    name:"应用属性",
                    group:'5',
                    callback:(data)=>{
                        this.$refs.appInfo.open(data)
                    }
                }
                ],
            menuNameFilter:(nameList,info)=>{
                if(info.sourceType!==0) return ['运行','修改名称','应用属性']
                return nameList
            }
        }
    },
    created() {

    },
    mounted() {},
    watch: {
    },
    methods: {
        itemClick(data){
            openWin(`/#/app/appDesign?id=${data.id}`,data.name,40,30);
        },
        addAppByBlank(){
            this.$refs.addOrEditApp.open()
        },
        addAppByLibrary(){},
        addAppByImport(){
            this.$parent.openLocalDialog()
        },
        copyApp(){
            //  const loading = this.$loading({
            //     lock: true,
            //     spinner: 'el-icon-loading',
            //     background: 'rgba(0, 0, 0, 0.7)'
            // });
            // this.api.appCopy({})
        }
    }
};
</script>

<style lang="scss" scoped>
  .my-apps{
    height: calc(100% - 76px);
  }
</style>